package concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 测试Future返回
 * @author luodan
 *
 */
public class FutureTest {
    ExecutorService executor = Executors.newCachedThreadPool();
    
    public FutureTest(){
        Future<?> future1 = executor.submit(task1);
        Future<Integer> future2 = executor.submit(task2);
        while (!future1.isDone() || !future2.isDone()) {
            System.out.println("waiting 1 :" + Thread.currentThread().getId());
        }
    }

    Runnable task1 = new Runnable(){
        
        @Override
        public void run(){
            System.out.println("run task1 :" + Thread.currentThread().getId());
        }
    };
    
    Callable<Integer> task2 = new Callable<Integer>(){

        @Override
        public Integer call() throws Exception{
            System.out.println("run task2 :" + Thread.currentThread().getId());
            return 2;
        }
        
    };
    
    
    public static void main(String[] args){
        new FutureTest();
    }
}
